package org.timeticker;

import junit.framework.TestCase;

import java.util.Date;

/**
 * Date Range unit test
 *
 * @author Ivan Latysh
 * @version 0.1
 * @since 25-Aug-2007 : 5:37:25 PM
 */
public class DateRangeUTest extends TestCase {

  DayRange dr;
  Day now = new Day();

  protected void setUp() throws Exception {
    dr = new DayRange();
    assertNotNull(dr);
    assertNull(dr.getFrom());
    assertNull(dr.getTo());
  }

  /**
   * Test get/set methods 
   */
  public void testGettersSetters() {
    dr.setFrom(now);
    assertEquals(now, dr.getFrom());
    dr.setTo(now);
    assertEquals(now, dr.getTo());
  }

  /**
   * Test null ranges
   */
  public void testNullRanges() {
    DayRange other = new DayRange();
    assertTrue(dr.equals(other));
    // set from date
    dr.setFrom(now);
    other.setFrom(now);
    assertTrue(dr.equals(other));
    // set to date
    dr.setFrom(null);
    other.setFrom(null);
    dr.setFrom(now);
    other.setFrom(now);
    assertTrue(dr.equals(other));
    // set both dates
    dr.setFrom(now);
    other.setFrom(now);
    dr.setFrom(now);
    other.setFrom(now);
    assertTrue(dr.equals(other));
  }

  /**
   * Test equal ranges
   */
  public void testEqualRanges() {
    DayRange another;
    
    // past days
    dr.setFrom(now);
    dr.setTo(now);
    another = new DayRange(new Date(now.getTime()-1000*60*10),  now);
    assertFalse(dr.equals(another));
    
    // 23 hours from now
    Day tomorrow = new Day(now.getTime()+1000*60*60*23);
    dr.setFrom(now);
    dr.setTo(tomorrow);
    another = new DayRange(new Date(now.getTime()+1000*60*10),  new Date(tomorrow.getTime()+1000*60*5));
    assertTrue(dr.equals(another));
  }

  /**
   * Test not equal ranges
   */
  public void testNotEqualRanges() {
    // 23 hours from now
    Day tomorrow = new Day(now.getTime()+1000*60*60*23);    
    dr.setFrom(now);
    dr.setTo(tomorrow);
    DayRange another = new DayRange(new Date(now.getTime()+1000*60*60*24),  new Date(tomorrow.getTime()+1000*60*5));
    assertTrue(dr.equals(another));
  }

}
